<template>
  <el-container class="layout-container-demo">
    <el-header class="header-container">
      <div class="logo">问卷系统</div>
      <div class="toolbar">
        <el-avatar :src="'http://192.168.61.163:9090'+avatarUrl" class="user-avatar" />
        <el-dropdown>
          <el-icon style="margin-right: 16px; cursor: pointer;">
            🍀<setting />
          </el-icon>
          <template #dropdown>
            <el-dropdown-menu>
              <router-link to="/information">
                <el-dropdown-item>🤫个人中心</el-dropdown-item>
              </router-link>
              <el-dropdown-item @click.native="handleLogout">😠退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
        <span>👈</span>
      </div>
    </el-header>
  </el-container>
</template>


<script lang="ts">
import { ref } from 'vue';
import request from '../utils/request';
// import { Menu as IconMenu, Message, Setting } from '@element-plus/icons-vue'; // 这行放图标

export default {
  data() {
    return {
      avatarUrl: '', 
    };
  },
  methods: {

    async loadUserAvatar() {
      try {
        const id = localStorage.getItem('userid');
        const response = await request.get('api/user/select', { params: { id: id } });
        this.avatarUrl = response.data[0].avatar; // 假设后端返回的用户数据中包含头像URL
      } catch (error) {
        console.error('加载用户头像失败', error);
      }
    },

    handleLogout() {
      this.$message.success('退出登录成功');
      localStorage.clear();
      this.$router.push('/');
    },
  },
  mounted() {
    this.loadUserAvatar();
  },
};
</script>

<style scoped>
.layout-container-demo {
  width: 100%;
  height: 100vh; /* Ensure it takes the full height of the viewport */
  display: flex;
  flex-direction: column;
}

.layout-container-demo .el-header {
  width: 100%; /* Ensure the header takes the full width */
  position: relative;
  background-color: var(--el-color-primary-light-7);
  color: var(--el-text-color-primary);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.header-container {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
}

.logo {
  font-size: 24px;
  font-weight: bold;
  color: var(--el-color-primary);
}

.toolbar {
  display: flex;
  align-items: center;
}

.toolbar .el-icon {
  font-size: 24px;
}

.toolbar span {
  font-size: 24px;
  margin-left: 10px;
}
</style>
